import {useEffect, useState} from "react";
import {Button, Image, Form, Modal, Row, message} from "antd";
import {ApiFindInfo} from "@/api";
import useCard from "@/hooks/useCard";
import useBoolean from "@/hooks/useBoolean";
import DndTable from "@/components/common/table/dndTable";
import UploadFile from "@/components/common/form/uploadFile";
import {CustomFormItem, InputFormItem} from "@/components/common/form/formItem";
import {columnsIndex, columnsRelevanceId, columnsSetting} from "@/components/Columns";

const TYPE = 0;

export default function Swiper() {
    const [form] = Form.useForm();
    const [visibly, on, off] = useBoolean(false);
    const [row, setRow] = useState<any>("");
    const [img, setImg] = useState("");
    const [data, setData] = useState([]);
    const [onDel, onCreateOrEdit] = useCard({});

    const columns = [
        columnsIndex,
        columnsRelevanceId,
        {
            title: "图片链接",
            dataIndex: "imgUrl",
            key: "imgUrl",
            render: (src: any) => <Image width={100} src={src} />,
        },
        {
            ...columnsSetting,
            render: (_: any, row: any) => (
                <div>
                    <Button type="primary" size="small" onClick={() => handleEdit(row)}>
                        编辑
                    </Button>

                    <Button danger onClick={() => handleDel(row)} style={{marginLeft: 20}} size="small">
                        删除
                    </Button>
                </div>
            ),
        },
    ];

    const handleEdit = (row: any) => {
        setRow(row);
        setImg(row.imgUrl);
        on();
        form.setFieldsValue(row);
    };

    const handleDel = (row: any) => {
        const params = [{pkId: row.pkId}];
        onDel(params, reqFindList);
    };

    const handleOk = async () => {
        try {
            const val = form.getFieldsValue();
            const arr = [{type: TYPE, ...val, pkId: row.pkId, imgUrl: img}];
            const res: any = await onCreateOrEdit(arr);
            setImg("");
            if (res) {
                reqFindList(onCancel);
            }
        } catch (error) {
            console.log(error);
        }
    };

    const onCancel = () => {
        setRow("");
        off();
    };

    //查找列表
    const reqFindList = (callBack?: any) => {
        ApiFindInfo({type: TYPE})
            .then((res: any) => {
                setData(res.data);
                callBack && callBack();
            })
            .catch((err: any) => {
                message.error(err);
            });
    };

    useEffect(() => {
        reqFindList();
    }, []);

    return (
        <>
            <Row>
                <Button type="primary" onClick={on}>
                    添加轮播
                </Button>
                &nbsp;&nbsp;
                <Button type="primary" onClick={() => onCreateOrEdit(data)}>
                    保存排序
                </Button>
            </Row>
            <DndTable data={data} setData={setData} style={{marginTop: 20}} columns={columns} />
            <Modal
                destroyOnClose
                width={500}
                title={row ? "编辑轮播" : "添加轮播"}
                open={visibly}
                onOk={handleOk}
                onCancel={onCancel}
            >
                <Form form={form} labelAlign="left" labelCol={{span: 4}} preserve={false}>
                    <InputFormItem width={300} name="rid" label="id" />
                    <CustomFormItem name="imgUrl" label="图片地址" shouldUpdate>
                        {img ? (
                            <>
                                <Image
                                    src={img}
                                    alt="avatar"
                                    style={{
                                        width: "100%",
                                        maxWidth: 300,
                                    }}
                                />
                                <div>
                                    <Button style={{marginTop: 10}} onClick={() => setImg("")} danger>
                                        删除图片
                                    </Button>
                                </div>
                            </>
                        ) : (
                            <UploadFile setImg={setImg} />
                        )}
                    </CustomFormItem>
                </Form>
            </Modal>
        </>
    );
}
